package com.qlylkj.lbk.ui.cochain

import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.qlylkj.lbk.R
import com.qlylkj.lbk.app.PreferenceHelper
import com.qlylkj.lbk.base.BaseVMFragment
import kotlinx.android.synthetic.main.fragment_cochain_record.*
import me.bakumon.statuslayoutmanager.library.StatusLayoutManager

class CoChainRecordFragment: BaseVMFragment<CoChainViewModel>() {

    companion object{
        fun newInstance(): CoChainRecordFragment {
            return CoChainRecordFragment()
        }
    }

    private val coChainRecordAdapter by lazy { CoChainRecordAdapter() }

    private val statusLayoutManager by lazy { initStatusLayoutManager() }

    override fun providerVMClass() = CoChainViewModel::class.java

    override fun setLayoutId() = R.layout.fragment_cochain_record

    override fun initView() {
        refreshLayout.apply {
            setOnRefreshListener {
                getCoChainRecord(false)
            }
            setOnLoadMoreListener {
                getCoChainRecord(true)
            }
        }
        coChainRecordRecyclerView.apply {
            layoutManager = LinearLayoutManager(activity)
            adapter = coChainRecordAdapter
        }
    }

    private fun initStatusLayoutManager(): StatusLayoutManager {
        return StatusLayoutManager.Builder(coChainRecordRecyclerView).apply {
            setDefaultEmptyImg(R.mipmap.bg_no_data)
            setDefaultEmptyText("暂无上链记录")
            setDefaultEmptyClickViewVisible(false)
        }.build()
    }

    override fun initData() {

    }

    override fun onResume() {
        super.onResume()
        getCoChainRecord(true)
    }

    override fun startObserve() {
        super.startObserve()
        mViewModel.apply {
            uiState.observe(this@CoChainRecordFragment, Observer { uiModel ->
                uiModel?.run {
                    refreshLayout.finishRefresh()
                    refreshLayout.finishLoadMore()
                    if(coChainInfoList.isNotEmpty()){
                        if(isRefresh){
                            refreshLayout.resetNoMoreData()
                            coChainRecordAdapter.replaceData(coChainInfoList)
                        }else{
                            coChainRecordAdapter.addData(coChainInfoList)
                        }
                    }else{
                        refreshLayout.finishLoadMoreWithNoMoreData()
                        refreshLayout.setEnableFooterFollowWhenNoMoreData(true)
                    }
                    if(coChainRecordAdapter.data.isEmpty()){
                        statusLayoutManager.showEmptyLayout()
                    }else{
                        statusLayoutManager.showSuccessLayout()
                    }
                }
            })
        }
    }

    private fun getCoChainRecord(isRefresh: Boolean){
        PreferenceHelper.getUserInfo({
            mViewModel.getCoChainInfoList(it.id,isRefresh)
        })
    }

}